package com.milkshopbackend.milkshop.controller;

import com.milkshopbackend.milkshop.Service.ProductSpecService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.entity.ProductSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductSpecController {

    @Autowired
    private ProductSpecService productSpecService;

    @PostMapping("/{productId}/specs")
    public Result<?> addSpec(@PathVariable Long productId, @RequestBody ProductSpec spec) {
        spec.setProductId(productId);
        productSpecService.addSpec(spec);
        return Result.success();
    }

    @PutMapping("/specs/{specId}")
    public Result<?> updateSpec(@PathVariable Long specId, @RequestBody ProductSpec spec) {
        spec.setId(specId);
        productSpecService.updateSpec(spec);
        return Result.success();
    }

    @DeleteMapping("/specs/{specId}")
    public Result<?> deleteSpec(@PathVariable Long specId) {
        productSpecService.deleteSpec(specId);
        return Result.success();
    }

    @GetMapping("/{productId}/specs")
    public Result<List<ProductSpec>> getSpecs(@PathVariable Long productId) {
        return Result.success(productSpecService.getSpecsByProductId(productId));
    }
}